package com.sihai.ehr.web.action.leave;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.*;
import com.sihai.ehr.model.sys.AuditStatus;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.model.sys.User;
import com.sihai.ehr.service.*;
import com.sihai.ehr.utils.DateUtil;
import com.sihai.ehr.web.AuthorityManager;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.Iterator;
import java.util.List;

/**
 * author: wxn
 * since: 13-4-14 下午12:32
 * 行政签核系统 - 出差申请单Action
 */
@Results({
        @Result(name = "add", location = "leave_project_input.jsp"),
        @Result(name = "list", location = "leave_project_list.jsp"),
        @Result(name = "main", location = "leave_office_main.jsp")
})
public class LeaveOfficeProjectAction extends EHRAction {
    @BeanInject
    private LeaveOfficeProjectService leaveOfficeProjectService;
    @BeanInject
    private EmployeeService employeeService;
    @BeanInject
    private UserService userService;
    @BeanInject
    private OrganizationService organizationService;

    private LimitedList<LeaveOfficeProject> projects;
    private LeaveOfficeProject project;

    private List<Organization> organizations;

    @Action("/leave/project-main")
    public String main() throws Exception {
        organizations = organizationService.findChildren(null, true, null, null);
        if (organizations != null && organizations.size() > 0) {
        	if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
            organizationId = organizations.get(0).getId();
        }
        listAction = "/leave/project-list.xhtml";
        listTitle = "离职项目管理";
        return "main";
    }

    @Action("/leave/project-list")
    public String list() throws Exception {
        projects = leaveOfficeProjectService.findByTopOrganization(organizationId, createLimit(), createSorter());
        this.setPageUrl("leave/project-list.xhtml");
        this.paging(projects);
        return "list";
    }

    @Action("/leave/project-create")
    public String toAdd() throws Exception {
        return "add";
    }

    @Action("/leave/project-edit")
    public String toEdit() throws Exception {
        if(project != null && project.getId() != null){
            project = leaveOfficeProjectService.get(project.getId());
            organizationId = project.getOrganization().getId();
        }

        return "add";
    }

    //保存
    @Action("/leave/project-save")
    public String save() throws Exception {
        if(project == null){
            return main();
        }
        if(project.getId() != null && project.getId() > 0){ //edit
            LeaveOfficeProject p = leaveOfficeProjectService.get(project.getId());
            p.setName(project.getName());
            p.setContent(project.getContent());
            p.setAuditor(project.getAuditor());
            p.setRemark(project.getRemark());
            leaveOfficeProjectService.save(p);
        }else{
            leaveOfficeProjectService.save(project);
        }
        return main();
    }

    @Action("/leave/project-delete")
    public String delete() throws Exception {
        String deleteIds = getParameter("project_ids");
        if(StringUtils.isNotBlank(deleteIds)){
            leaveOfficeProjectService.delete(deleteIds);
        }
        return main();
    }

    public LimitedList<LeaveOfficeProject> getProjects() {
        return projects;
    }

    public List<Organization> getOrganizations() {
        return organizations;
    }

    public LeaveOfficeProject getProject() {
        return project;
    }

    public void setProject(LeaveOfficeProject project) {
        this.project = project;
    }
}
